﻿using System;
using System.Drawing;
using CommonLibrary.Models;
using ShuntingScheduleUserControl.Helper;
using ShuntingScheduleUserControl.Model;

namespace ShuntingScheduleUserControl.View
{
    public class ViewShuntingSchedule
    {
        /// <summary>
        /// 序号
        /// </summary>
        public int Index { get; }
        /// <summary>
        /// 总数
        /// </summary>
        public int Sum { get; }
        public Schedule Schedule { get; }
        public DrawContext DrawContext { get; }

        public RectangleF Rectangle { get; set; }

        public float XRight { get; set; }

        public float XLeft { get; set; }

        public float Y { get; set; }

        public ViewShuntingSchedule(int index, int sum, Schedule shuntingSchedule, DrawContext drawContext)
        {
            Index = index;
            Sum = sum;
            Schedule = shuntingSchedule;
            DrawContext = drawContext;

            CalculateShapes();
        }

        private void CalculateShapes()
        {
            Y = DrawContext.GetScheduleY(Schedule.MachId);
            XLeft = DrawContext.GetScheduleX(Schedule.StartTime);
            XRight = DrawContext.GetScheduleX(Schedule.EndTime);

            if (Schedule.PosId == 0)
            {
                Rectangle = new RectangleF(XLeft, Y - DrawContext.ShuntingScheduleHeight, XRight - XLeft, DrawContext.ShuntingScheduleHeight * 2);
            }
            else if (Schedule.PosId == 1)//上
            {
                Rectangle = new RectangleF(XLeft, Y - (float)DrawContext.ShuntingScheduleHeight / 2 - 5, XRight - XLeft, DrawContext.ShuntingScheduleHeight - 2);
            }
            else if (Schedule.PosId == 2)//下
            {
                Rectangle = new RectangleF(XLeft, Y + 2, XRight - XLeft, DrawContext.ShuntingScheduleHeight - 2);
            }
        }

        private float DrawShuntingSchedule(Graphics g, Pen pen)
        {
            if (Math.Abs(XLeft - XRight) > 0.1 && Y != 0)
            {
                g.DrawRectangle(pen, Rectangle.X, Rectangle.Y, Rectangle.Width, Rectangle.Height);
                g.DrawString(Schedule.JobId.ToString(), DrawHelper.FtDefault, DrawHelper.BrushShuntingScheduleName, Rectangle);
            }

            return Y;
        }

        public void Draw(Graphics g)
        {
            DrawShuntingSchedule(g, DrawHelper.PenGreen1);
        }

        public bool IsInRange(Point point)
        {
            return Rectangle.Contains(point);
        }
    }
}